package com.waijiaojun.tpo.crawler;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Peter on 2018/2/7.
 */
public class CrawlerTopUrls {
    public static void main(String[] args) throws IOException {
        List<TpoXzDto> urlList = getOgDtos();

        for (TpoXzDto o : urlList) {
            System.out.println(o);
        }
    }

    public static List<TpoXzDto> getTpoXzDtos() throws IOException {
        List<TpoXzDto> urlList = new ArrayList<>();
        Document document = Jsoup.connect("http://top.zhan.com/toefl/listen/alltpo.html").get();
        Elements a = document.select(".tpo_list a");
        for (Element e : a) {
            String href = "http://top.zhan.com" + e.attr("href");
            System.out.println("\n" + href);

            // tpo_list
            Document doc_tpo_list = Jsoup.connect(href).get();
            Elements e_titles = doc_tpo_list.select(".tpo_desc_item_list>.title");//tpo53
            for (int i = 0; i < e_titles.size(); i++) {
                Element e_t = e_titles.get(i);
                String tpoName = e_t.text().split(" ")[0].trim();
                System.out.println(tpoName);
                Element e_tpoN_list = e_t.nextElementSibling();// tpo53 list
                Elements e_tpoN_item = e_tpoN_list.select(".tpo_desc_item");
                for (int j = 0; j < e_tpoN_item.size(); j++) {

                    Elements item_img = e_tpoN_item.get(j).select(".item_img");
                    String cover_url = item_img.select("img").get(0).attr("src");
                    String tpoName_item = tpoName + "." + (j + 1);
                    //System.out.println("  tpoName_item:" + tpoName_item);
                    //System.out.println("      cover_url - " + cover_url);
                    String scenUrl = "http://top.zhan.com" + item_img.select("a").get(2).attr("href");
                    //System.out.println("      scenUrl - " + scenUrl);
                    urlList.add(new TpoXzDto(tpoName_item, scenUrl, cover_url));
                }
            }
        }
        return urlList;
    }

    public static List<TpoXzDto> getExtraDtos() throws IOException {
        List<TpoXzDto> urlList = new ArrayList<>();
        Document document = Jsoup.connect("http://top.zhan.com/toefl/listen/extra.html").get();
        Elements a = document.select(".tpo_list a");
        for (Element e : a) {
            String href = "http://top.zhan.com" + e.attr("href");
            System.out.println("\n" + href);

            // tpo_list
            Document doc_tpo_list = Jsoup.connect(href).get();
            Elements e_titles = doc_tpo_list.select(".tpo_desc_item_list>.title");//tpo53
            for (int i = 0; i < e_titles.size(); i++) {
                Element e_t = e_titles.get(i);
                String tpoName = e_t.text().split(" ")[0].trim();
                System.out.println(tpoName);
                Element e_tpoN_list = e_t.nextElementSibling();// tpo53 list
                Elements e_tpoN_item = e_tpoN_list.select(".tpo_desc_item");
                for (int j = 0; j < e_tpoN_item.size(); j++) {

                    Elements item_img = e_tpoN_item.get(j).select(".item_img");
                    String cover_url = item_img.select("img").get(0).attr("src");
                    String tpoName_item = tpoName + "." + (j + 1);
                    //System.out.println("  tpoName_item:" + tpoName_item);
                    //System.out.println("      cover_url - " + cover_url);
                    String scenUrl = "http://top.zhan.com" + item_img.select("a").get(2).attr("href");
                    //System.out.println("      scenUrl - " + scenUrl);
                    urlList.add(new TpoXzDto(tpoName_item, scenUrl, cover_url));
                }
            }
        }
        return urlList;
    }

    public static List<TpoXzDto> getOgDtos() throws IOException {
        List<TpoXzDto> urlList = new ArrayList<>();
        String href = "http://top.zhan.com/toefl/listen/og.html";
        System.out.println("\n" + href);

        // tpo_list
        Document doc_tpo_list = Jsoup.connect(href).get();
        Elements e_titles = doc_tpo_list.select(".tpo_desc_item_list>.title");//tpo53
        for (int i = 0; i < e_titles.size(); i++) {
            Element e_t = e_titles.get(i);
            String tpoName = e_t.text().split(" ")[0].trim();
            System.out.println(tpoName);
            Element e_tpoN_list = e_t.nextElementSibling();// tpo53 list
            Elements e_tpoN_item = e_tpoN_list.select(".tpo_desc_item");
            for (int j = 0; j < e_tpoN_item.size(); j++) {

                Elements item_img = e_tpoN_item.get(j).select(".item_img");
                String cover_url = item_img.select("img").get(0).attr("src");
                String tpoName_item = tpoName + "." + (j + 1);
                //System.out.println("  tpoName_item:" + tpoName_item);
                //System.out.println("      cover_url - " + cover_url);
                String scenUrl = "http://top.zhan.com" + item_img.select("a").get(2).attr("href");
                //System.out.println("      scenUrl - " + scenUrl);
                urlList.add(new TpoXzDto(tpoName_item, scenUrl, cover_url));
            }
        }
        return urlList;
    }

}
